package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsCheckTicketDao;
import com.sugon.dao.ToolsCheckTicketItemDao;
import com.sugon.entity.ToolsCheckTicketEntity;
import com.sugon.service.ToolsCheckTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测工单Service实现类
 *
 * @author sugon
 * @date 2021-09-27 10:47:49
 */
@Service("toolsCheckTicketService")
public class ToolsCheckTicketServiceImpl implements ToolsCheckTicketService {

    @Autowired
    private ToolsCheckTicketDao toolsCheckTicketDao;
    @Autowired
    private ToolsCheckTicketItemDao toolsCheckTicketItemDao;

    @Override
    public ToolsCheckTicketEntity queryObject(String id) {
        return toolsCheckTicketDao.queryObject(id);
    }

    @Override
    public List<ToolsCheckTicketEntity> queryList(Map<String, Object> map) {
        return toolsCheckTicketDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsCheckTicketDao.queryTotal(map);
    }

    @Override
    public int save(ToolsCheckTicketEntity toolsCheckTicket) {
        toolsCheckTicket.setId(RandomUtil.randomString(32));
        toolsCheckTicket.setGmtCreate(DateUtil.date());
        return toolsCheckTicketDao.save(toolsCheckTicket);
    }

    @Override
    public int update(ToolsCheckTicketEntity toolsCheckTicket) {
        toolsCheckTicket.setGmtModified(DateUtil.date());
        return toolsCheckTicketDao.update(toolsCheckTicket);
    }

    @Override
    public boolean commitMeasureData(ToolsCheckTicketEntity toolsCheckTicket) {
        // 需要判断当前工单下是否含有未处理不合格项，则需要修改工单状态
        int unTreatedNum = toolsCheckTicketItemDao.countUnTreatedItem(toolsCheckTicket.getId());
        if (unTreatedNum > 0) {
            toolsCheckTicket.setIsTreated("1");
        }
        return update(toolsCheckTicket) > 0;
    }

    @Override
    public int delete(String id) {
        return toolsCheckTicketDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return toolsCheckTicketDao.deleteBatch(ids);
    }
}
